from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
from scipy.signal import find_peaks


def plot3C(d, title=''):
    plt.figure(figsize=(12,3))
    plt.subplot(311)
    plt.title(title)
    plt.plot(d[0], linewidth=0.5)
    plt.subplot(312)
    plt.plot(d[1], linewidth=0.5)
    plt.subplot(313)
    plt.plot(d[2], linewidth=0.5)
    plt.show()

def plot_ps(d, title='p,s prob'):
    print(np.argmax(d[0]), np.argmax(d[1]))
    plt.figure(figsize=(10, 3))
    plt.subplot(211)
    plt.title(title)
    plt.ylim(0,1.1)
    plt.plot(d[0])
    plt.subplot(212)
    plt.ylim(0,1.1)
    plt.plot(d[1])
    plt.show()


def preprocess(data):
    # data[0] = data[0]-np.mean(data[0])
    # data[1] = data[1]-np.mean(data[1])
    # data[2] = data[2]-np.mean(data[2])
    data -= np.mean(data, axis=0, keepdims=True)
    if (np.std(data) != 0):
        data = data/(np.std(data)+1e-6)
    return data


def detect_peaksV2(x, height=None, distance=0):
    ind, _ = find_peaks(x, height, distance=distance)
    # print(ind)
    # if ind.size and removehead > 0:
    #     # print('ind < removehead:',ind, ind < removehead)
    #     ind = ind[ind > removehead]
    return ind, x[ind].tolist()

snr_low=-1
snr_high=3
snr_least=0.4
def snr2weight(snr):
    if snr>snr_high:
        return 1
    if snr<snr_low:
        return snr_least
    return (snr-snr_low)/(snr_high-snr_low)+snr_least
